#coding="utf-8"
#----------------------------------------------------
# Target: 测试so.com
# Author: Terry
# From : 2023-11-24
#----------------------------------------------------

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import unittest


# 定义常量
CONST_URL='https://www.so.com'
CONST_WAIT=3


# # 定义浏览器参数
# myoptions=webdriver.ChromeOptions()
# myoptions.add_argument('--ignore-ssl-error')
# driver=webdriver.Chrome(myoptions)


#region 单独执行测试功能
# 尝试执行
# try:

#     r= driver.get(CONST_URL)
#     assert "360搜索" in driver.title

#     time.sleep (CONST_WAIT)

#     einput = driver.find_element(by=By.NAME,value='q')
#     esearch = driver.find_element(by=By.ID,value='search-button')

#     einput.clear()                        
#     einput.send_keys('WIFI7')
#     # einput.send_keys(Keys.ENTER) # 使用场景少
#     esearch.click() #使用场景多
#     assert "找到相关结果约" in driver.page_source

#     print("任务执行顺利")
# except Exception as e:
#     print(e)
# finally:
#     driver.close()

#endregion



#region # 使用框架实现测试

class Sopage(unittest.TestCase):
    

    def setUp(self):
        myoptions=webdriver.ChromeOptions()
        myoptions.add_argument('--ignore-ssl-error')
        self.driver=webdriver.Chrome(myoptions)
    
    def tearDown(self):
       self.driver.close()
    
    def test_search(self):
        driver=self.driver
        driver.get(CONST_URL)
        assert "360搜索" in driver.title

        # 搜索wifi7
        einput = driver.find_element(by=By.NAME,value='q')
        esearch = driver.find_element(by=By.ID,value='search-button')
        einput.clear()                        
        einput.send_keys('WIFI7')
        # einput.send_keys(Keys.ENTER) # 使用场景少
        esearch.click() #使用场景多
        # assert "找到相关结果约" in driver.page_source
        driver.implicitly_wait(2)
        assert 'No result found ' not in driver.page_source

        time.sleep(CONST_WAIT)
        # driver.save_screenshot('screenshot.png')
        # 搜索空格
        driver.refresh()
        sinput=driver.find_element(by=By.ID,value='keyword')
        sinput.clear()                        
        sinput.send_keys(' ')
        sinput.send_keys(Keys.ENTER) # 使用场景少
        driver.implicitly_wait(2)
        driver.save_screenshot('screenshot.png')
        assert "靠谱" in driver.page_source



if __name__=='__main__':
    unittest.main()
#endregion